package com.springboot.love.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.love.dao.UserDao;
import com.springboot.love.entity.User;
import com.springboot.love.service.UserService;
import com.springboot.love.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private UserDao userDao;


    @RequestMapping("/userAll")
    public String noticeAll(){
        return "admin/listUser";
    }
    /**
     * 查询所以帖子
     * @return
     */
    @RequestMapping(value = {"/userList"}, method = RequestMethod.GET)
    @ResponseBody
    public String postList(Model model, String page, String limit){
        log.info("查询全部用户");
        PageUtil pageUtil=new PageUtil(Integer.parseInt(page),Integer.parseInt(limit));
        List<User> userList = userService.queryAllByTime(pageUtil);
        int userTotal=userService.queryTotalByTime();
        JSONObject json = new JSONObject();
        json.put("code",0);
        json.put("data",userList);
        json.put("count", userTotal);
        json.put("status", 200);
        return json.toString();
    }

    /**
     * 根据id修改性别状态
     */
    @RequestMapping(value = {"/sexStatus"}, method = RequestMethod.POST)
    @ResponseBody
    public void  sexStatus(String userId,String sex){
        log.info(sex.equals("0")?"男":"女");
        User user = new User();
        user.setUserId(Integer.parseInt(userId));
        user.setSex(Byte.parseByte(sex));
        userDao.updateById(user);
    }

    /**
     * 根据id修改帖子状态
     */
    @RequestMapping(value = {"/userStatus"}, method = RequestMethod.POST)
    @ResponseBody
    public void  userStatus(String userId,String userState){
        log.info(userState.equals("2")?"成功":"待审核");
        User user = new User();
        user.setUserId(Integer.parseInt(userId));
        user.setUserState(Byte.parseByte(userState));
        userDao.updateById(user);
    }

    @RequestMapping("updatepass")
    public String updatepass(){
        return "admin/updatePass";
    }
    /**
     * 修改密码
     */
    @RequestMapping(value = "rePassword", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> rePassword(String userId,String pass,String repass){
        log.info("用户 {} 修改密码",userId);
        Map<String, Object> map = new HashMap<>();
        int i = userService.checkPass(userId, pass, repass);
        switch (i){
            case 1:
                map.put("code",1);
                break;
            case 3:
                map.put("code",3);
                break;
            case 4:
                map.put("code",4);
                break;
            default:
                if (userService.modifyPass(userId,pass)){
                    map.put("code",0);
                    log.info("用户 {} 修改密码成功",userId);
                }else {
                    log.info("用户 {} 修改密码失败",userId);
                    map.put("code",10);
                }
                break;
        }
        return map;
    }
}
